#include "Player.h"
#include "CGlobal.h"
#include "ConstDefine.h"
using namespace cocos2d;
CPlayer::CPlayer(void)
{
	
	bInvincible_=false;
	bSafecap_=false;
	bArmor_=false;
	bGhost_=false;
	bPlane_=false;
}

CPlayer::~CPlayer(void)
{
}

CPlayer * CPlayer::node( const std::string arrayPic[],int nNum,float fIntervalTime ,bool bRepeat/*=true*/ )
{
	CPlayer *pNode=new CPlayer;
	if (pNode)
	{
		pNode->autorelease();
		if (pNode->init(arrayPic,nNum,fIntervalTime,bRepeat))
		{
			return pNode;
		}
	}
	return NULL;
}

bool CPlayer::init( const std::string arrayPic[],int nNum,float fIntervalTime ,bool bRepeat/*=true*/ )
{
	if (!CAnimationSprite::init(arrayPic,nNum,fIntervalTime,bRepeat))
	{
		return false;
	}
	setTransparent(false);
	return true;
	
}

cocos2d::CCSize CPlayer::getSize(cocos2d::CCSize scaleSize)
{
	CCSize size(121.0f*CGlobal::GetRatioX(),126.0f*CGlobal::GetRatioY());
	return CCSize(size.width*scaleSize.width,size.height*scaleSize.height);
}

void CPlayer::update(float fTime )
{
	b2Body *pBody=getPhysicsBody();
	if (pBody)
	{
		b2Vec2 b2Pos=pBody->GetPosition();
		setPosition(VEC2PT(b2Pos));
	}
	if (bInvincible_)
	{
		setTransparent(true);
	}
}



void CPlayer::setInvincible( bool bInvincible )
{
	bInvincible_=bInvincible;
	CCBlink *pAction1=CCBlink::create(2,15);
	CCBlink *pAction2=CCBlink::create(2,10);
	CCBlink *pAction3=CCBlink::create(2,5);
	CCBlink *pAction4=CCBlink::create(2,3);
	if (pAction1&&pAction2&&pAction3&&pAction4)
	{
		CCFiniteTimeAction *pActionSeq=CCSequence::actions(pAction1,pAction2,pAction3,pAction4,
			CCCallFuncN::actionWithTarget(this,callfuncN_selector(CPlayer::callfuncAction)), NULL);
		if (pActionSeq)
		{
			pActionSeq->setTag(ACTION_BLINK);
			this->runAction(pActionSeq);
		}
	}
	
	

}

void CPlayer::callfuncAction(cocos2d::CCNode *pNode)
{
	CCAction *pAction =getActionByTag(ACTION_BLINK);
	if (pAction)
	{
		this->stopAction(pAction);
	}
	bInvincible_=false;
	setTransparent(false);
}

bool CPlayer::isSafecap()
{
	return bSafecap_;
}

void CPlayer::setSafecap( bool bSafecap )
{
	bSafecap_=bSafecap;
	if (bSafecap)
	{
		CCSprite *pSafecap=CGlobal::SpriteWithFile("safecap.png");
		if (pSafecap)
		{
			addChild(pSafecap,0,CHILD_TAG_SAFECAP);
		}
	}
	else
	{
		CCSprite *pSafecap=(CCSprite *)getChildByTag(CHILD_TAG_SAFECAP);
		if (pSafecap)
		{
			removeChild(pSafecap,true);
		}
	}
	
	
}

bool CPlayer::isArmor()
{
	return bArmor_;
}

void CPlayer::setArmor( bool bArmor )
{
	bArmor_ =bArmor;
	if (bArmor)
	{
		CCSprite *pArmor=CGlobal::SpriteWithFile("armor.png");
		if (pArmor)
		{
			addChild(pArmor,0,CHILD_TAG_ARMOR);
		}
		CCDelayTime *pDelayTime=CCDelayTime::actionWithDuration(5);
		CCCallFuncN *pCallFunc= CCCallFuncN::actionWithTarget(this,callfuncN_selector(CPlayer::callbackArmor));
		if (pDelayTime&&pCallFunc)
		{
			CCFiniteTimeAction *pSeq=CCSequence::actions(pDelayTime,pCallFunc,NULL);
			if (pSeq)
			{
				this->runAction(pSeq);
			}
		}
	}
	else
	{
		CCSprite *pArmor=(CCSprite *)getChildByTag(CHILD_TAG_ARMOR);
		if (pArmor)
		{
			removeChild(pArmor,true);
		}
	}

}

bool CPlayer::isGhost()
{
	return bGhost_;
}

void CPlayer::setGhost( bool bGhost )
{
	bGhost_=bGhost;
	CCBlink *pAction1=CCBlink::create(2,15);
	CCBlink *pAction2=CCBlink::create(2,10);
	CCBlink *pAction3=CCBlink::create(2,5);
	CCBlink *pAction4=CCBlink::create(2,3);
	if (pAction1&&pAction2&&pAction3&&pAction4)
	{
		CCFiniteTimeAction *pActionSeq=CCSequence::actions(pAction1,pAction2,pAction3,pAction4,
			CCCallFuncN::actionWithTarget(this,callfuncN_selector(CPlayer::callfunGhostEnd)), NULL);
		if (pActionSeq)
		{
			pActionSeq->setTag(ACTION_BLINK);
			this->runAction(pActionSeq);
		}
	}
}

void CPlayer::callfunGhostEnd( cocos2d::CCNode *pNode )
{
	CCAction *pAction =getActionByTag(ACTION_BLINK);
	if (pAction)
	{
		this->stopAction(pAction);
	}
	bGhost_=false;
	setTransparent(false);
}

bool CPlayer::isInvincible()
{
	return bInvincible_;
}

void CPlayer::callbackArmor(CCNode* pNode)
{
	CCSprite *pArmor= (CCSprite*)getChildByTag(CHILD_TAG_ARMOR);
	if (pArmor)
	{
		removeChild(pArmor,true);
	}
	bArmor_=false;
}

bool CPlayer::isPlane()
{
	return bPlane_;
}

void CPlayer::setPlane( bool bPlane )
{
	bPlane_=bPlane;
}